package com.iot.hmi.controller;

import com.iot.hmi.entity.DeviceInfo;
import com.iot.hmi.utils.SqliteHelper;
import javafx.fxml.FXML;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.TextField;

import java.sql.SQLException;

public class DeviceInfoDialogController {

    @FXML
    private TextField collectorBoxIdField;
    @FXML
    private TextField deviceNameField;
    @FXML
    private TextField assetIdField;

    private DeviceInfo deviceInfo;
    private boolean isConfirmed = false;
    private Dialog<ButtonType> dialog;
    private int deviceId;

    public void setDialog(Dialog<ButtonType> dialog) {
        this.dialog = dialog;
        initializeDialog();
    }

    public void setDeviceInfo(DeviceInfo deviceInfo) {
        this.deviceInfo = deviceInfo;
        if (deviceInfo != null) {
            collectorBoxIdField.setText(deviceInfo.getCollectorBoxId());
            deviceNameField.setText(deviceInfo.getDeviceName());
            assetIdField.setText(deviceInfo.getAssetId());
        }
    }

    public DeviceInfo getDeviceInfo() {
        return deviceInfo;
    }

    public boolean isConfirmed() {
        return isConfirmed;
    }

    private void initializeDialog() {
        if (dialog != null) {
            dialog.setResultConverter(buttonType -> {
                if ("OK_DONE".equals(buttonType.getButtonData().toString())) {
                    isConfirmed = true;
                    if (deviceInfo == null) {
                        deviceInfo = new DeviceInfo();
                    }
                    deviceInfo.setCollectorBoxId(collectorBoxIdField.getText());
                    deviceInfo.setDeviceName(deviceNameField.getText());
                    deviceInfo.setAssetId(assetIdField.getText());
                    saveDeviceInfo();
                    return buttonType;
                } else if (("CANCEL_CLOSE").equals(buttonType.getButtonData().toString())) {
                    // 处理取消操作
                    isConfirmed = false;
                    return buttonType;
                }
                return null;
            });
            // 处理关闭请求
            dialog.setOnCloseRequest(event -> {
                dialog.setResult(ButtonType.CANCEL);
            });
        }
    }

    @FXML
    void saveDeviceInfo(int deviceId) {
        this.deviceId = deviceId;
    }

    public void saveDeviceInfo() {
        try {
            if (deviceInfo.getId() == null || deviceInfo.getId() == 0) {
                deviceInfo.setDeviceId(deviceId);
                SqliteHelper.insertDeviceInfo(deviceId, deviceInfo.getCollectorBoxId(), deviceInfo.getDeviceName(), deviceInfo.getAssetId());
            } else {
                // 更新设备信息的逻辑
                SqliteHelper.updateDeviceInfo(deviceInfo.getId(), deviceInfo.getDeviceId(), deviceInfo.getCollectorBoxId(), deviceInfo.getDeviceName(), deviceInfo.getAssetId());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
